/*
  Applicast API Extension/Utility Library
  Date manipulation utilities

  (c) 2009 ILERRED, S.L.

  Applicast API Extension Library is freely
  distributable under the terms of an MIT-style license.
*/
var DateUtil = new Object();
Object.extend(DateUtil, {
  CultureInfo: {      
    //Day Name Strings
    dayNames: ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"],
    abbreviatedDayNames: ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"],
    shortestDayNames: ["do", "lu", "ma", "mi", "ju", "vi", "sá"],
    firstLetterDayNames: ["d", "l", "m", "m", "j", "v", "s"],
      
    //Month Name Strings
    monthNames: ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"],
    abbreviatedMonthNames: ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"],
  
  	//AM/PM Designators
    amDesignator: "",
    pmDesignator: ""
  },
  
  getDateFromTimestamp: function(timestamp) {
    var d = new Date();
    if ( timestamp )
      d.setTime(timestamp*1000);
    return d;
  },

  getDayName: function (date, abbrev) {
    return abbrev ? DateUtil.CultureInfo.abbreviatedDayNames[date.getDay()] :  DateUtil.CultureInfo.dayNames[date.getDay()];
  },
  
  getMonthName: function (date, abbrev) {
    return abbrev ? DateUtil.CultureInfo.abbreviatedMonthNames[date.getMonth()] :  DateUtil.CultureInfo.monthNames[date.getMonth()];
  },
    
  toJSONString: function (date) {
    return date.getUTCFullYear()  +  '-' +
      (date.getUTCMonth() + 1).toPaddedString(2) + '-' +
      date.getUTCDate().toPaddedString(2) + ' ' + 
      date.getUTCHours().toPaddedString(2) + ':' +
      date.getUTCMinutes().toPaddedString(2) + ':' + 
      date.getUTCSeconds().toPaddedString(2) + ' ' +
      (date.getTimezoneOffset() < 0 ? '-' : '+') + Math.abs(date.getTimezoneOffset()/60).toPaddedString(2) + '00';    
  },

  toISO8061String: function(date) {
    return date.getUTCFullYear()  +  '-' +
      (date.getUTCMonth() + 1).toPaddedString(2) + '-' +
      date.getUTCDate().toPaddedString(2) + 'T' + 
      date.getUTCHours().toPaddedString(2) + ':' +
      date.getUTCMinutes().toPaddedString(2) + ':' + 
      date.getUTCSeconds().toPaddedString(2) + 
      (date.getTimezoneOffset() < 0 ? '-' : '+') + Math.abs(date.getTimezoneOffset()/60).toPaddedString(2) + ':00';
  },
  
  toLongDateString: function(date) {
    return DateUtil.getDayName(date) + ', ' + 
      date.getDate().toPaddedString(2) + ' de ' + 
      DateUtil.getMonthName(date) + ' de ' + 
      date.getFullYear();
  },
  
  toShortDateTimeString: function(date) {
    return date.getDate().toPaddedString(2) + '/' +
      (date.getMonth()+1).toPaddedString(2) + '/' + 
      date.getFullYear() + ' ' +
      date.getHours().toPaddedString(2) + ':' + 
      date.getMinutes().toPaddedString(2); 
  },
  
  toLongDateTimeString: function(date) {
    return DateUtil.getDayName(date, true) + ', ' + 
      date.getDate().toPaddedString(2) + ' de ' + 
      DateUtil.getMonthName(date, true) + ' de ' + 
      date.getFullYear() + ' ' + 
      date.getHours().toPaddedString(2) + ':' + 
      date.getMinutes().toPaddedString(2) + ':' + 
      date.getSeconds().toPaddedString(2);    
  },
  
  format: function(date, formatString) {
    var handler = {
      Y: function(d) { return d.getFullYear().toString(); },
      y: function(d) { return d.getFullYear().toString().slice(-2); },
      m: function(d) { return ('0' + (d.getMonth() + 1)).slice(-2); },
      d: function(d) { return ('0' + d.getDate()).slice(-2); },
      e: function(d) { return d.getDate(); },
      H: function(d) { return ('0' + d.getHours()).slice(-2); },
      M: function(d) { return ('0' + d.getMinutes()).slice(-2); },
      S: function(d) { return ('0' + d.getSeconds()).slice(-2); },
      a: function(d) { return DateUtil.getDayName(d, true); },
      A: function(d) { return DateUtil.getDayName(d, false); },
      b: function(d) { return DateUtil.getMonthName(d, true); },
      B: function(d) { return DateUtil.getMonthName(d, false); }
    };
    return formatString.replace(/%([a-zA-Z%])/g, function(_, c) {
      return handler[c](date);
    });
  },
  
  toGmailFormat: function(date) {
    var today = new Date();
    var f1 = DateUtil.format(today, '%d%m');
    var f2 = DateUtil.format(date, '%d%m');
    
    if ( f1 === f2 ) return DateUtil.format(date, '%H:%M');
    return DateUtil.format(date, '%e / %b');
  }
});